using System;
using System.Globalization;
using System.Threading;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EntLibContrib.Resource;

namespace EntLibContrib.WebResourceQuickStart
{
	public partial class _Default : System.Web.UI.Page
	{
		#region Fields
		// Todays date used to set the default message of the day
		private DateTime today = System.DateTime.Today;
		// Array of supported Cultures containing English, French and German
		private CultureInfo[] cultures = new CultureInfo[3] { new CultureInfo("en-GB"), new CultureInfo("fr-FR"), new CultureInfo("de-DE") };
		// Resource Manager holding the message resources. This is the default resource manager
		// in configuration, hence no need to give the instance name.  This is also a Binary resource
		// which means that the resources come directly from the "*.resources" files.
		private ResourceManager messageResources = ResourceFactory.GetResourceManager();
		#endregion

		#region Event Handlers
		/// <summary>
		/// Handles the Load event of the Page control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
				// initialise the drop down lists
				InitializeCultures();
		}

		/// <summary>
		/// Handles the SelectedIndexChanged event of the comboCulture control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void comboCulture_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// get the culture box
			DropDownList cultureBox = (DropDownList)sender;
			// change the current culture to the selected culture
			InitializeCulture(new CultureInfo(cultureBox.SelectedValue));
		}

		/// <summary>
		/// Handles the SelectedIndexChanged event of the comboDays control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		protected void comboDays_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// change the message of the day text and its label to the selected day
			MessageOfTheDay();
		}

		/// <summary>
		/// Sets the <see cref="P:System.Web.UI.Page.Culture"/> and <see cref="P:System.Web.UI.Page.UICulture"/> for the current thread of the page.
		/// </summary>
		protected override void InitializeCulture()
		{
			if (Session["Culture"] == null)
			{
				Session["Culture"] = CultureInfo.InvariantCulture;
				// set thread culture
				Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
				// set page culture
				UICulture = Culture = CultureInfo.InvariantCulture.Name;
			}
			else
			{
				// set thread culture
				Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = (CultureInfo)Session["Culture"];
				// set page culture
				UICulture = Culture = ((CultureInfo)Session["Culture"]).Name;
			}
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Set the message of the day.
		/// </summary>
		/// <remarks>
		/// The message of the day label is fetched from form resources using a strongly typed class,
		/// while the message of the day text is fetched from message resources using an indexer.
		/// </remarks>
		public void MessageOfTheDay()
		{
			// Message of the day label. Note the use of the generated strongly typed class
			// this class was generated from the Enterprise Library configuration tool
			//string messageLabel = PageResources.labelMessageOfTheDay_Text;
			this.messageLabel.Text = PageResources.messageLabelResource1_Text + this.comboDays.SelectedItem.Text;

			// Message of the day text. Here we use string manipulation to calculate the resource key
			// and then apply the key to the resource manager default indexer for string resources.
			this.message.Text = messageResources[this.comboDays.SelectedValue + "Message"];
		}
		#endregion

		#region Private Methods
		/// <summary>
		/// Initializes the current thread culture.
		/// </summary>
		/// <param name="culture">The culture.</param>
		private void InitializeCulture(CultureInfo culture)
		{
			// save the current culture
			Session["Culture"] = culture;
			// initialise the thread and page culture
			InitializeCulture();
			// initialise the days drop down list
			InitializeDays();
			// this is required to force the page to re-initialize with the new culture
			Server.Transfer(Request.Path);
		}

		/// <summary>
		/// Initializes the supported cultures combo box.
		/// </summary>
		private void InitializeCultures()
		{
			// initialize the culture drop down list
			this.comboCulture.DataSource = cultures;
			this.comboCulture.DataTextField = "NativeName";
			this.comboCulture.DataValueField = "Name";
			this.comboCulture.DataBind();
			// set the selected list item, this is necessary because to force the page to refresh
			// in order to change the culture we have to do a Server.Transfer which in turn loses
			// the view state so we need to use session state to set the correct selected item
			if (Session["Culture"] != null)
			{
				CultureInfo culture = (CultureInfo)Session["Culture"];
				if (culture != CultureInfo.InvariantCulture)
					this.comboCulture.SelectedValue = culture.Name;
			}
			// initialise the days drop down list
			InitializeDays();
		}

		/// <summary>
		/// Initializes the days of the week combo.
		/// </summary>
		private void InitializeDays()
		{
			// create a bindable generic list of day names
			List<DayName> dayNames = new List<DayName>(7);
			DayName dayName;
			DayName todayName = null;

			// populate the list of day names
			foreach (string dayOfTheWeek in System.Enum.GetNames(typeof(DayOfWeek)))
			{
				// get the day name for the current page culture
				dayName = new DayName(dayOfTheWeek, messageResources[dayOfTheWeek]);
				dayNames.Add(dayName);
				if (dayOfTheWeek == today.DayOfWeek.ToString())
					todayName = dayName;
			}

			// bind the list of day names to the combo box
			this.comboDays.DataSource = dayNames;
			this.comboDays.DataTextField = "NameValue";
			this.comboDays.DataValueField = "NameKey";
			this.comboDays.DataBind();
			// set the selected day to today.
			this.comboDays.SelectedValue = todayName.NameKey;
			MessageOfTheDay();
		}
		#endregion
	}
}
